package br.edu.fatec.tutorial2d;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import br.edu.fatec.tutorial2d.GraphicObject.Coordinates;
import br.edu.fatec.tutorial2d.GraphicObject.Speed;

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

	private TutorialThread tutorialThread;
	private List<GraphicObject> graphicObjects = new ArrayList<GraphicObject>();
	private GraphicObject currentGraphic;
	
	public Panel(Context context) {
		super(context);
		getHolder().addCallback(this);
		
		tutorialThread = new TutorialThread(this, getHolder());
		setFocusable(true);
	}

	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		Bitmap bitmap;
		Coordinates coordinates;
		for (GraphicObject g : graphicObjects) {
			bitmap = g.getBitmap();
			coordinates = g.getCoordinates();
			canvas.drawBitmap(bitmap, coordinates.getX(), coordinates.getY(), null);
		}
		
		if (currentGraphic != null) {
			bitmap = currentGraphic.getBitmap();
			coordinates = currentGraphic.getCoordinates();
			canvas.drawBitmap(bitmap, coordinates.getX(), coordinates.getY(), null);
		}
		
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		tutorialThread.setRunning(true);
		tutorialThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		tutorialThread.setRunning(false);
		
		while(retry) {
			try {
				tutorialThread.join();
				retry = false;
			}catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		synchronized (tutorialThread.getSurfaceHolder()) {
			GraphicObject graphic;
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
				graphic.getCoordinates().setX((int) event.getX() - graphic.getBitmap().getWidth() / 2);
				graphic.getCoordinates().setY((int) event.getY() - graphic.getBitmap().getHeight() / 2);
				currentGraphic = graphic;
			}else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				currentGraphic.getCoordinates().setX((int) event.getX() - currentGraphic.getBitmap().getWidth() / 2);
				currentGraphic.getCoordinates().setY((int) event.getY() - currentGraphic.getBitmap().getHeight() / 2);
			}else if (event.getAction() == MotionEvent.ACTION_UP) {
				graphicObjects.add(currentGraphic);
				currentGraphic = null;
			}
			return true;
		}
	}
	
	public void updatePhysics() {
		Coordinates coordinates;
		Speed speed;
		for (GraphicObject g : graphicObjects) {
			coordinates = g.getCoordinates();
			speed = g.getSpeed();
			
			if (speed.getXDirection() == Speed.X_DIRECTION_RIGHT) {
				coordinates.setX(coordinates.getX() + speed.getX());
			}else {
				coordinates.setX(coordinates.getX() - speed.getX());
			}
			
			if (speed.getYDirection() == Speed.Y_DIRECTION_DOWN) {
				coordinates.setY(coordinates.getY() + speed.getY());
			}else {
				coordinates.setY(coordinates.getY() - speed.getY());
			}
			
			if (coordinates.getX() < 0) {
				speed.toggleXDirection();
				coordinates.setX(-coordinates.getX());
			}else if (coordinates.getX() + g.getBitmap().getWidth() > getWidth()) {
				speed.toggleXDirection();
				coordinates.setX(coordinates.getX() + getWidth() - (coordinates.getX() + g.getBitmap().getWidth()));
			}
			
			if (coordinates.getY() < 0) {
				speed.toggleYDirection();
				coordinates.setY(-coordinates.getY());
			}else if (coordinates.getY() + g.getBitmap().getHeight() > getHeight()) {
				speed.toggleYDirection();
				coordinates.setY(coordinates.getY() + getHeight() - (coordinates.getY() + g.getBitmap().getHeight()));
			}
		}
	}
	
}
